<template>
	<view class="container align">
		<view class="" style="width: 100%;">
			
		
		<view class="backcenter center">
			<!-- <image src="http://123.56.189.119:9000/manghe/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20231107223450.png" mode="widthFix" class="bg"></image> -->
			<view class="loginbox ">
				<view class="title colortext">
					欢迎登录
				</view>
				<button @click="bindGetUserInfo()" class="btn1 btn">
					微信一键登录
				</button>
				<!-- <button class="btn2 btn">
					验证码登录
				</button> -->
			</view>
		</view>
		
		<view class="center" style="margin-top: 50rpx;">
			免责协议
		</view>
		</view>
	</view>

</template>

<script>
	import { getAuthInfo, wxlogin,dylogin } from "@/api/login"
	// import { getLoginInfo } from "@/api/user"
	import { setToken, getToken, removeToken,setData, getData, removeData,} from "@/utils/auth"
	// import {appAddInvitee, appSelectInviteeList} from "@/api/invitee"
	export default{
		data(){
			return{
				wxCode:'',
				sessionKey: '',
				openId: '',
				nickName: null,
				avatarUrl: null,
				userInfo:{},
				canIUse: uni.canIUse('button.open-type.getUserInfo'),
				canIGetUserProfile:false,
				checkVal:'1',
				titleText:"",
				checked:false,
				isShow:false,
				exemption:null,
				inviteeId:""
			}
		},
		onLoad(option) {
			// if(option != null && option != ""){
			// 	this.inviteeId = "test";
			// 	const q = decodeURIComponent(option)
			// 	console.log("da",q)
			// 	uni.showToast({
			// 		title:option
			// 	})
			// }
			var _this = this;
			//判断一下token
			let token = getToken();
			if(token!=null && token!=""){
				//如果token不为null 跳转到首页
				uni.switchTab({
					url: '/pages/index/index'
				});
			}
			//_this.getInfo();
			if( uni.getUserProfile ){  
				this.canIGetUserProfile = true;  
			}  
			//判断若是版本不支持新版则采用旧版登录方式
			//查看是否授权
			if( !this.canIGetUserProfile){  
				uni.getSetting({
					success: function(res) {
						if (res.authSetting['scope.userInfo']) {
							uni.getUserInfo({
							  provider: 'weixin',
							  success: function(res) {
								console.log(res);
								_this.userInfo = res.userInfo;
								try {
								  _this.login();
								} catch (e) {}
							  },
							  fail(res) {}
							});
						} else {
							// 用户没有授权
							console.log('用户还没有授权');
						}
					}
				});
			}
		},
		methods:{
			bindGetUserInfo(e){
				var _this = this;
				if(this.canIGetUserProfile){
					//新版登录方式
					uni.getUserProfile({
						desc:'登录',
						success:(res)=>{
							console.log(res);
							_this.userInfo = res.userInfo;
							try {
								_this.login();
							} catch (e) {}
						},
						fail:(res)=>{
							console.log(res)
						}
					});
				}else{
					//旧版登录方式
					if (e.detail.userInfo) {
						//用户按了允许授权按钮
						//console.log('手动');
						//console.log(e.detail.userInfo);
						_this.userInfo = e.detail.userInfo;
						try {
							_this.login();
						} catch (e) {}
					} else {
						console.log('用户拒绝了授权');
						//用户按了拒绝按钮
					}
				}
			},
			//登录
			login() {
				let _this = this;
				// 获取登录用户code
				uni.login({
					provider: 'weixin',
					success: function(res) {
						console.log(res);
						if(res.code){
						   let code = res.code;
						   _this.wxCode=res.code
						   console.log('code');
						   console.log(code);
						   _this.updateUserInfo();
						    //将用户登录code传递到后台置换用户SessionKey、OpenId等信息
							//..写用code置换SessionKey、OpenId的接口
						  //  getAuthInfo(code.toString()).then((response)=>{
							 //   let data = response.data;
							 //   console.log(response);
								// if(data.code==200){
								// 	let info = JSON.parse(data.msg);
								// 	console.log(info);
								// 	_this.openId = info.openid;
								// 	//置换成功调用登录方法_this.updateUserInfo();
									
								// }
						  //  })
						}else{
							uni.showToast({title: '登录失败！',duration: 2000});
							console.log('登录失败！' + res.errMsg)
						}
				  },
				});
			},
			 //向后台更新信息
			updateUserInfo() {
				uni.showLoading({
					title: '登录中...'
				});
				let _this = this;
				var params = {
					username: _this.openId,
					openId: _this.openId,
					nickName: _this.userInfo.nickName,
					avatarUrl: _this.userInfo.avatarUrl,
				}
				console.log("wxCode")
				console.log(_this.wxCode)
				//...后台登录的接口
				dylogin(_this.wxCode).then(response =>{
					//登录成功
					console.log(response.data.code);
					if(response.data.code == 200){
						console.log("login",response)
						//存储token信息
						setToken(response.data.data);
						//获取用户信息
						// _this.getInfo();
						
						//跳转到首页
						uni.switchTab({
							url: '/pages/index/index'
						});
					}
				});
				uni.hideLoading();
			},
			checkboxChange(e){
				if(e.detail.value=='1'){
					this.checked = true;
				}else{
					this.checked = false
				}
			},
			closeIsShow(){
				this.isShow = false;  
			},
			getInfo(){
				getLoginInfo().then((response)=>{
					if(response.data.code == 200){
						console.log("user",response.data.user)
						//存储data信息
						setData(response.data.user);
						// //拉新
						// if(this.inviteeId != null && this.inviteeId != ""){
						// 	let querParams = {
						// 		rewardType : '1',
						// 		userId : response.data.user.userId,
						// 		inviteeId : this.inviteeId
						// 	}
						// 	appAddInvitee(querParams).then(res => {
						// 		console.log("ldads",res)
						// 	})
							
						// }
					}
				})
			}
		}
	}
</script>

<style scoped>
	.container {
		background: url(http://123.56.189.119:9000/manghe/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20231107223450.png) no-repeat;
		height: 99vh;
		background-size: 100% 100%;
		/* display: flex;
		justify-content: center;
		align-items: center;
		text-align: center;
		position: relative; */
	}
	.backcenter{
		/* height: 96vh; */
		/* display: flex;
		justify-content: center;
		align-items: center;
		text-align: center;
		position: relative; */
	}
	.bg {
		position: absolute;
		width: 100%;
	}

	.loginbox {
		background: rgba(255, 255, 255, .8);
		width: 80%;
		border-radius: 15px;
		padding: 60rpx 30rpx;
	}

	.title {
		text-align: center;
		font-size: 60rpx;
		letter-spacing: 5rpx;
		font-weight: 700;
		margin: 40rpx 0 160rpx;
	}

	.btn {
		margin: 30rpx 0;
		padding: 0 !important
	}
</style>
